<template>
  <el-dialog
      v-model="dialogVisible"
      :title="title"
      width="30%"
      :before-close="handleClose"
  >
    <div v-if="!isPassPhone">
      <el-form
          ref="ruleFormRef"
          :model="pwdForm"
          status-icon
          :rules="rules"
          label-width="120px"
          class="pwdForm"
      >
        <el-form-item prop="phone">
          <el-input v-model="pwdForm.phone" clearable size="large" placeholder="请输入手机号码" autocomplete="off" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="pwdForm.password" clearable size="large" placeholder="请输入新密码" autocomplete="off" />
        </el-form-item>
        <el-form-item prop="code">
          <el-input
              v-model="pwdForm.code"
              type="password"
              autocomplete="off"
              placeholder="请输入验证码"
              size="large"
          >
            <template #suffix>
              <TencentCaptcha scene="2" :phone="pwdForm.phone" :phoneRule="validatePhone"></TencentCaptcha>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button round type="primary"  clearable size="large" @click="submitForm(ruleFormRef)"
          >下一步</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div v-else>
      <el-form
          ref="pwdFormRef"
          :model="updatePwdForm"
          status-icon
          :rules="pwdRules"
          label-width="120px"
          class="pwdForm"
      >
        <el-form-item prop="password">
          <el-input v-model="updatePwdForm.password" clearable size="large" placeholder="请输入密码" autocomplete="off" />
        </el-form-item>
        <el-form-item prop="cheackPwd">
          <el-input
              v-model="updatePwdForm.cheackPwd"
              type="password"
              autocomplete="off"
              placeholder="请再次输入密码"
              size="large"
          >
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button round type="primary"  clearable size="large" @click="updatePwdFormSubmit(pwdFormRef)"
          >完成</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </el-dialog>
</template>

<script>
  import TencentCaptcha from "../../../components/TencentCaptcha/TencentCaptcha";
import {reactive, toRefs, watchEffect} from "vue";
  import {ElMessage} from "element-plus";
  import {meRevisePassword} from "@/api/api";

export default {
  name: "index",
  props:['dialogVisible'],
  emits:['done'],
  setup(props,context){
    let data = reactive({
      title : '通过手机号找回密码',
      isPassPhone : false,// 是否输入手机号并且通过
      ruleFormRef : null,
      pwdFormRef:null,
      pwdForm:{
        phone:'',
        password:'',
        code:''
      },
      updatePwdForm:{
        password:'',
        cheackPwd:''
      }
    })

    const validatePhone = (rule, value, callback) => {
      let reg = /^1[23456789]\d{9}$/;
      if (value === '') {
        callback(new Error('手机号码不能为空'))
      } else if (!reg.test(value)) {
        callback(new Error("手机号码格式错误"))
      } else {
        callback()
      }
    }

    const rules = reactive({
      phone: [{ validator: validatePhone, trigger: 'blur' }],
      password: [{ required: true, min: 6, max: 6,message:'密码长度为6位', trigger: 'blur' }],
      code: [{ required: true,message:'不能为空', trigger: 'blur' }],
    })
    const pwdRules = reactive({
      phone: [{ required: true,message:'不能为空', trigger: 'blur' }],
      code: [{ required: true,message:'不能为空', trigger: 'blur' }],
    })
    // console.log('dialogVisible',props.dialogVisible)
    const handleClose = () => {
      context.emit('done',false)
    }
    watchEffect(() =>{
      if(data.isPassPhone){
        data.title = '手机号码'+data.pwdForm.phone
      }
    })
    const submitForm = (formEl) => {
      if (!formEl) return
      formEl.validate((valid) => {
        if (valid) {
          console.log('submit!')
          meRevisePassword(data.pwdForm).then(res =>{
            console.log(res)
            props.dialogVisible = false;
            ElMessage({type:'success',message:res.message})
          }).catch(err =>{
            ElMessage({type:'error',message:err.message})
          })
        } else {
          console.log('error submit!')
          return false
        }
      })
    }

    const updatePwdFormSubmit = (formEl) => {
      if (!formEl) return
      formEl.validate((valid) => {
        if (valid) {
          console.log('submit!')
        } else {
          console.log('error submit!')
          return false
        }
      })
    }

    return{
      validatePhone,
      handleClose,
      submitForm,
      updatePwdFormSubmit,
      rules,
      pwdRules,
      ...toRefs(data)
    }
  },
  components:{
    TencentCaptcha
  }
}
</script>

<style>
.pwdForm .el-form-item__content{
  margin-left: 0px !important;
}
.pwdForm .el-button{
  width: 100%;
}
</style>

<style scoped>

</style>
